package com.halixun.d2app.dialog;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.halixun.d2app.R;
import com.halixun.d2app.utils.ToastUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class IntervalTimeFragment extends DialogFragment implements View.OnClickListener {

    private TextView time;

    private IntervalTimeListener mListener;

    public IntervalTimeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_interval_time, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListeners(view);
    }

    private void setListeners(View view) {
        view.findViewById(R.id.confirm).setOnClickListener(this);
        view.findViewById(R.id.cancel).setOnClickListener(this);
    }

    private void initView(View view) {
        time = view.findViewById(R.id.time);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.confirm:
                String text = time.getText().toString().trim();
                if (TextUtils.isEmpty(text)){
                    ToastUtils.showCenterToast("请输入间隔时间");
                    return;
                }
                if (null!=mListener){
                    mListener.onIntervalTimeListener(Long.valueOf(text));
                }
            case R.id.cancel:
                dismiss();
                break;
        }
    }

    public void setIntervalTimeListener(IntervalTimeListener listener){
        this.mListener = listener;
    }

    public interface IntervalTimeListener{
        void onIntervalTimeListener(long time);
    }

}
